package com.adcc.loadsheet.monitor.entity;

import com.adcc.loadsheet.monitor.util.AlarmEnum;
import com.adcc.loadsheet.monitor.util.StringUtil;
import com.adcc.loadsheet.monitor.vo.ForwarderMonitorVO;

import java.util.HashSet;
import java.util.Set;

public class ForwarderMonitor extends BaseEntity<ForwarderMonitorVO> {

    // 第一个程序的IP
    private String firstHost;

    // 第一个程序是否存活
    private boolean firstProgramUp;

    // 第一个程序的航旅接口是否存活
    private boolean firstUmeInterfaceUp;

    // 第二个程序的IP
    private String secondHost;

    // 第二个程序是否存活
    private boolean secondProgramUp;

    // 第二个程序的航旅接口是否存活
    private boolean secondUmeInterfaceUp;

    // 服务状态
    private MonitorStatus status = MonitorStatus.ABNORMAL;

    // 异常状态码(0：正常，1：异常，2：告警)
    private String errorCode = "0";

    // 异常原因：XXX IS DOWN,XXX IS DOWN
    private String errorReason = "";

    // 本模块异常的节点
    private Set<String> alarmContent = new HashSet<>();

    // 本模块正常的节点（用于判断告警确认后，部分节点正常后又异常需要重新告警）
    private Set<String> unAlarmContent = new HashSet<>();

    @Override
    public Object clone() {
        ForwarderMonitor forwarder = new ForwarderMonitor();
        forwarder.setFirstHost(firstHost);
        forwarder.setFirstProgramUp(firstProgramUp);
        forwarder.setFirstUmeInterfaceUp(firstUmeInterfaceUp);
        forwarder.setSecondHost(secondHost);
        forwarder.setSecondProgramUp(secondProgramUp);
        forwarder.setSecondUmeInterfaceUp(secondUmeInterfaceUp);
        forwarder.setStatus(status);
        forwarder.setErrorCode(errorCode);
        forwarder.setErrorReason(errorReason);
        forwarder.setAlarmContent(alarmContent);
        forwarder.setUnAlarmContent(unAlarmContent);
        return forwarder;
    }

    /**
     * 异常状态码
     */
    public void buildErrorCode() {
        StringBuilder sb = new StringBuilder();
        char[] states = new char[4];
        states[0] = firstProgramUp ? '0' : '1';
        states[1] = firstUmeInterfaceUp ? '0' : '1';
        states[2] = secondProgramUp ? '0' : '1';
        states[3] = secondUmeInterfaceUp ? '0' : '1';
        for (char c : states) {
            sb.append(c);
        }
        this.errorCode = sb.toString();

        // [2024-6-01：张丽娟：增加声音和弹窗告警的功能，系统支持对指定告警项进行声音和弹窗告警忽略的功能: 根据状态码获取正常节点和异常节点信息]
        // 本模块异常的节点
        Set<String> alarmContent = new HashSet<>();
        // 本模块正常的节点（用于判断告警确认后，部分节点正常后又异常需要重新告警）
        Set<String> unAlarmContent = new HashSet<>();
        // 保存到数据库的异常信息
        StringBuilder errorReasonBuilder = new StringBuilder();

        AlarmEnum enumFirstPorgramUp = AlarmEnum.FORWARDER_FIRSTPORGRAMUP;
        if (states[0] == '1') {
            alarmContent.add(enumFirstPorgramUp.getItem());
        }else{
            unAlarmContent.add(enumFirstPorgramUp.getItem());
        }

        AlarmEnum enumFirstUmeinterfaceUp = AlarmEnum.FORWARDER_FIRSTUMEINTERFACEUP;
        if (states[1] == '1') {
            alarmContent.add(enumFirstUmeinterfaceUp.getItem());
        }else{
            unAlarmContent.add(enumFirstUmeinterfaceUp.getItem());
        }

        AlarmEnum enumSecondPorgramUp = AlarmEnum.FORWARDER_SECONDPORGRAMUP;
        if (states[2] == '1') {
            alarmContent.add(enumSecondPorgramUp.getItem());
        }else{
            unAlarmContent.add(enumSecondPorgramUp.getItem());
        }

        AlarmEnum enumSecondUmeinterfaceUp = AlarmEnum.FORWARDER_SECONDUMEINTERFACEUP;
        if (states[3] == '1') {
            alarmContent.add(enumSecondUmeinterfaceUp.getItem());
        }else{
            unAlarmContent.add(enumSecondUmeinterfaceUp.getItem());
        }

        this.alarmContent = alarmContent;
        this.unAlarmContent = unAlarmContent;
        this.errorReason = StringUtil.stringBuilderToString(errorReasonBuilder);
    }

    @Override
    public String toXML() throws Exception {
        return null;
    }

    @Override
    public String toJSON() throws Exception {
        return null;
    }

    public String getFirstHost() {
        return firstHost;
    }

    public void setFirstHost(String firstHost) {
        this.firstHost = firstHost;
    }

    public boolean isFirstProgramUp() {
        return firstProgramUp;
    }

    public void setFirstProgramUp(boolean firstProgramUp) {
        this.firstProgramUp = firstProgramUp;
    }

    public boolean isFirstUmeInterfaceUp() {
        return firstUmeInterfaceUp;
    }

    public void setFirstUmeInterfaceUp(boolean firstUmeInterfaceUp) {
        this.firstUmeInterfaceUp = firstUmeInterfaceUp;
    }

    public String getSecondHost() {
        return secondHost;
    }

    public void setSecondHost(String secondHost) {
        this.secondHost = secondHost;
    }

    public boolean isSecondProgramUp() {
        return secondProgramUp;
    }

    public void setSecondProgramUp(boolean secondProgramUp) {
        this.secondProgramUp = secondProgramUp;
    }

    public boolean isSecondUmeInterfaceUp() {
        return secondUmeInterfaceUp;
    }

    public void setSecondUmeInterfaceUp(boolean secondUmeInterfaceUp) {
        this.secondUmeInterfaceUp = secondUmeInterfaceUp;
    }

    public MonitorStatus getStatus() {
        return status;
    }

    public void setStatus(MonitorStatus status) {
        this.status = status;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorReason() {
        return errorReason;
    }

    public void setErrorReason(String errorReason) {
        this.errorReason = errorReason;
    }

    public Set<String> getAlarmContent() {
        return alarmContent;
    }

    public void setAlarmContent(Set<String> alarmContent) {
        this.alarmContent = alarmContent;
    }

    public Set<String> getUnAlarmContent() {
        return unAlarmContent;
    }

    public void setUnAlarmContent(Set<String> unAlarmContent) {
        this.unAlarmContent = unAlarmContent;
    }
}
